package com.vxb.testmvp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.vxb.testmvp.test_fragment.LoadingPager;

import static java.security.AccessController.getContext;

public abstract class BaseActivity extends AppCompatActivity {
    public LoadingPager loadingPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        loadingPage = new LoadingPager(this,
                R.layout.loadpage_loading, R.layout.loadpage_error,
                R.layout.loadpage_empty) {
            @Override
            protected LoadResult load() {
                return BaseActivity.this.load();
            }

            @Override
            protected View createSuccessView() {
                return BaseActivity.this.createSuccessView();
            }
        };
        //		可以点击
        loadingPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadingPage.show();
            }
        });
        //		显示 loading的页面
        loadingPage.show();
        setContentView(loadingPage);
    }

    /**
     * 刷新页面工程
     *
     * @return
     */
    protected abstract View createSuccessView();

    /**
     * 请求服务器 获取当前状态
     */
    protected abstract LoadingPager.LoadResult load();


    protected void launch(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    public <T extends View> T mfindViewById(int resId) {
        return (T) findViewById(resId);
    }

    // 防止快速点击
    private long lastClick = 0;

    private boolean fastClick() {
        if (System.currentTimeMillis() - lastClick <= 1000) {
            return false;
        }
        lastClick = System.currentTimeMillis();
        return true;
    }


}
